﻿using Core.SimpleTemp.Common;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories.Internal;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;

namespace Core.SimpleTemp.Repositories.RepositoryEntityFrameworkCore.Sys
{
    [AutoDi()]
    public class SysDepartmentRepository : BaseDBRepository<SysDepartment>
    {
        public SysDepartmentRepository(CoreDBContext dbContext) : base(dbContext)
        {

        }

        /// <summary>
        /// 更新部门职务关联关系
        /// </summary>
        /// <param name="SysDepartmentID">部门Id</param>
        /// <param name="DepartmentDutys">职务集合</param>
        /// <returns></returns>
        public async Task<bool> UpdateDepartmentDutyAsync(Guid SysDepartmentID, List<SysDepartmentDuty> DepartmentDutys)
        {
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var oldDatas = await _dbContext.Set<SysDepartmentDuty>().Where(it => it.SysDepartmentID == SysDepartmentID).ToListAsync();
                oldDatas.ForEach(it => _dbContext.Set<SysDepartmentDuty>().Remove(it));
                await _dbContext.SaveChangesAsync();

                _dbContext.Set<SysDepartmentDuty>().AddRange(DepartmentDutys);
                await _dbContext.SaveChangesAsync();

                scope.Complete();
            }
            return true;
        }

       


    }

}
